﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 作业1_计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button18_Click(object sender, EventArgs e)
        {
            运算("-");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            double number = 0;
            double.TryParse(textBox1.Text, out number);
            textBox1.Text = (number * number).ToString();
            textBox2.AppendText(Environment.NewLine + number + "^2" );
            textBox2.Text = textBox2.Text + "=" + textBox1.Text;
            n2Cls = false;
        }
        /// <summary>
        /// 检查字符串是否为合法的数字
        /// </summary>
        /// <param name="str">输入的字符串</param>
        /// <param name="b">返回值，是否合法</param>
        /// <returns>如果合法返回转换后的值</returns>
        public double 检查字符串是否为数字(string str , ref bool b)
        {
            double number = 0;
            if (double.TryParse(str, out number))
            {
                b = true;
                return number;
            }
            else
            {
                b = false;
                return 0;
            }
                
        }

        public bool n2Cls = false;
        public string 第一个数;
        public string 运算符;
        public string 第二个数;
        public bool textBox3cls = false;


        public void 输入数字(string 值)
        {
            if (运算符 =="")
            {
                dealText(值);
            }
            else
            {
                if (false == n2Cls)
                {
                    textBox1.Text = "";
                    n2Cls = true;
                }
                dealText(值);
                if (false == textBox3cls)
                {
                    textBox3.Text = "";
                    textBox3cls = true;
                }

            }
         
        }

        public void dealText(string 值)
        {
            if ("+/-" == 值)
            {
                bool b = false;
                double n1 = 检查字符串是否为数字(textBox1.Text, ref b);
                textBox1.Text = ((-1) * n1).ToString();
            }
            else if ("." == 值)
            {
                if (textBox1.Text.Contains("."))
                {
                    return;
                }
                else
                {
                    textBox1.Text= textBox1.Text + 值;
                }
            }
            else
            {
                textBox1.Text = textBox1.Text + 值;
            }
        }
            
        private void button1_Click(object sender, EventArgs e)
        {
            输入数字("1");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            输入数字("2");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            输入数字("3");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            输入数字("4");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            输入数字("5");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            输入数字("6");
        }
        private void button7_Click(object sender, EventArgs e)
        {
            输入数字("7");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            输入数字("8");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            输入数字("9");
        }

        private void button14_Click(object sender, EventArgs e)
        {
            输入数字(".");
        }

        private void button12_Click(object sender, EventArgs e)
        {
            输入数字("+/-");
        }

        private void button13_Click(object sender, EventArgs e)
        {
            输入数字("0");
        }

        private void button17_Click(object sender, EventArgs e)
        {
            运算("+");
        }

        public void 运算(string 运算符1)
        {
            if ("" == textBox1.Text)
            {
                return;
           
            }
            第一个数 = textBox1.Text;
            运算符 = 运算符1;
            textBox3.Text= 第一个数 + 运算符 ;
            n2Cls = false;
            textBox3cls = true;
        }

        private void button15_Click(object sender, EventArgs e)
        {
            运算("*");
        }

        private void button16_Click(object sender, EventArgs e)
        {
            运算("/");
        }

        private void button11_Click(object sender, EventArgs e)
        {
            double number = 0;
            double.TryParse(textBox1.Text, out number);
            textBox1.Text = Math.Sqrt(number).ToString();
            textBox2.AppendText(Environment.NewLine + number + "^1/2");
            textBox2.Text = textBox2.Text + "=" + textBox1.Text;
            n2Cls = false;
        }

        private void button19_Click(object sender, EventArgs e)
        {
            // 点击等号
            bool b = 检查数据(第一个数, 运算符, textBox1.Text);
            if (false == b)
            {
                return;
            }
            textBox3.Text = textBox3.Text + textBox1.Text+"=";
            string 原数据 ;
            原数据 = textBox2.Text;
            textBox2.Clear();
            textBox2.AppendText(Environment.NewLine + 第一个数 + 运算符 + textBox1.Text);  
            //执行计算
            textBox1.Text = 计算(第一个数, 运算符, textBox1.Text).ToString();
            textBox2.Text = textBox2.Text + "=" + textBox1.Text;
            textBox2.AppendText(原数据);
            //MessageBox.Show(textBox2.TextLength.ToString());
            textBox2.ForeColor = Color.Blue;
            n2Cls = false;
            textBox3cls = false;
        }

        private void button20_Click(object sender, EventArgs e)
        {
            运算符 = "";
            textBox1.Text = "";
            第一个数 = ""; 
            textBox3.Text = "";
        }
        public bool 检查数据(string numbel1,string 运算符, string numbel2)
        {
            if(""== 运算符)
            {
                MessageBox.Show("请选择运算符");
                return false;
            }

            bool b = false;
            检查字符串是否为数字(numbel1, ref b);
            if (false == b)
            {
                MessageBox.Show("第一个参数不是合法的数字");
                return false;
            }
            检查字符串是否为数字(numbel2, ref b);
            if (false == b)
            {
                MessageBox.Show("第一个参数不是合法的数字");
                return false;
            }
            return true;
        }

        public double 计算(string numbel1, string 运算符, string numbel2)
        {
            bool b = false;
            double n1 = 检查字符串是否为数字(numbel1, ref b);
            double n2 = 检查字符串是否为数字(numbel2, ref b);
            switch (运算符)
            {
                case "+":
                    return n1 + n2;
                case "-":
                    return n1 - n2;
                case "*":
                    return n1 * n2;
                case "/":
                    return n1 / n2;
                default:
                    break;
            }

            return 0;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button10.Text = "x²";
            //button11.Text = "x½";
        }
    }
}
